package org.liang.service.impl;

import cn.hutool.core.lang.UUID;
import org.liang.clients.AuthClient;
import org.liang.constants.UserConstants;
import org.liang.domain.LoginUser;
import org.liang.domain.User;
import org.liang.domain.UserInfo;
import org.liang.dto.UserRegisterDto;
import org.liang.exception.BusinessException;
import org.liang.exception.ResponseCode;
import org.liang.exception.RonghuaAssert;
import org.liang.mapper.UserInfoMapper;
import org.liang.mapper.UserMapper;
import org.liang.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.liang.util.AjaxResult;
import org.liang.util.encrypt.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author liang
 * @since 2024-03-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private AuthClient authClient;

    @Override
    public void register(UserRegisterDto userRegisterDto) {
        //1.参数校验
        //非空校验----->jsr303校验
        //匹配验证码
        String phone = userRegisterDto.getPhone();
        String key = UserConstants.USER_REGISTER_SEND_SMS_KEY + ":" + phone;
        String codeInRedis = (String)redisTemplate.opsForValue().get(key);
        //判断redis存储的验证码是否过期
        RonghuaAssert.isNotBlank(codeInRedis, ResponseCode.RESPONSE_CODE_400009);
        String code = codeInRedis.split(":")[0];
        //判断验证码是否正确
        RonghuaAssert.isEq(code,userRegisterDto.getVerifyCode(),ResponseCode.RESPONSE_CODE_400009);

        //加盐加密
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        String password = MD5.getMD5("ronghua" + salt + userRegisterDto.getPassword());

        //保存数据到认证授权中心的t_login_user,获取login_id;
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(UUID.randomUUID().toString());
        loginUser.setPassword(password);
        loginUser.setSalt(salt);
        loginUser.setTel(phone);
        loginUser.setType(1);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if(!ajaxResult.isSuccess()){
            throw new BusinessException(ajaxResult.getMessage());
        }
        Long loginId = Long.parseLong(ajaxResult.getResultObj().toString());


        //存储数据到t_user
        User user = new User();
        user.setCreateTime(new Date().getTime());
        user.setLoginId(loginId);
        user.setBitState(1L);
        user.setPhone(phone);
        user.setSalt(salt);
        user.setPassword(password);
        user.setSecLevel(1);
        userMapper.insert(user);

        //存储到t_user_info
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setRegTime(new Date().getTime());
        userInfo.setGrowScore(0);
        userInfo.setLevel(0);
        userInfoMapper.insert(userInfo);
        //发送短信

    }
}
